import time, sys
import core_1
import core_3
from core_3 import SSD1306_I2C
from machine import UART, Pin, Timer, I2C
from core_2 import DHT22
'''
树莓派4B小程序-点亮LED灯
注意参考树莓派4B的引脚定义
原理：LED小灯分RGB三色，每个颜色一个阳极，阴极连接树莓派0V引脚。其余三个阳极分别连接树莓派14号、15号和18号引脚
首先要setup每个引脚，其次来控制高低电平。最后要运行GPIO.cleanup()函数，清除引脚信息。
'''


# dht22 GPIO 设置 - 以Pin.IN为准
dht_data = Pin(7, Pin.IN, Pin.PULL_UP)
dht_sensor = DHT22(dht_data, Pin(8, Pin.OUT), dht11=False)

# SSD1306 OLED屏幕 GPIO 设置 - 必须全局设置
i2c = I2C(1, sda=Pin(10), scl=Pin(11),freq=100000)
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

# 关机用触控按键 GPIO 设置 - 为长按两秒开关机提供触发
Touch_Btn_Reset = Pin(15, Pin.IN, None)

class Main:
    def __init__(self):
        super(Main, self).__init__()
        # TM1637 数码管 GPIO 设置 - 温度
        self.smg_temp = core_1.TM1637(clk=Pin(19),dio=Pin(20))
        # TM1637 数码管 GPIO 设置 - 湿度
        self.smg_humidity = core_1.TM1637(clk=Pin(26),dio=Pin(27))
        # LED GPIO 设置
        self.Blue = Pin(16, Pin.OUT)
        self.Red = Pin(17, Pin.OUT)
        self.Green = Pin(18, Pin.OUT)
        self.status_on = True
        
        # 长按两秒开关机的状态
        self.device_on = False
        
        #声明函数
        self.oled()
        self.get_temp("00.0")
        self.get_humid("00.0")
        self.dht22_visualization()

    def get_temp(self, temp):
        self.temp_front = int(temp[0:2])
        self.temp_end = int(temp[3:4])
        self.smg_temp.numbers(self.temp_front, self.temp_end)
        #print(self.temp_front)
        #print(self.temp_end)
        
    def get_humid(self, humidity):
        self.humidity_front = int(humidity[0:2])
        self.humidity_end = int(humidity[3:4])
        self.smg_humidity.numbers(self.humidity_front, self.humidity_end)
        # print(humidity)
    
    def dht22_visualization(self):
        self.T, self.H = dht_sensor.read()
        self.t = str(self.T)[0:4]
        self.h = str(self.H)[0:4]
        if self.device_on == False:
            pass
        else:
            self.get_temp(self.t)
            self.get_humid(self.h)

    def oled(self):
        oled.poweron()
        oled.fill(0)
        oled.text('Author: Lu', 10, 20)
        oled.text('siena.zone', 10, 36)
        oled.show()
        
    def led_turned_on(self):
        
        self.Blue.high()
        self.Red.high()
        self.Green.high()
        
        time.sleep(.5)
        
        self.Blue.low()
        self.Red.low()
        self.Green.low()
        
    def led_turned_off(self):
        
        self.Blue.low()
        self.Red.high()
        self.Green.low()
        
        time.sleep(.5)
        
        self.Red.low()
        
    def main(self):
        self.smg_temp.show("    ")
        self.smg_humidity.show("    ")
        oled.poweroff()
        while True:
            self.dht22_visualization()
            time.sleep(1)
            if Touch_Btn_Reset.value() == 1:
                if self.device_on == False:
                    self.device_on = True
                    self.oled()
                    self.led_turned_on()
                else:
                    self.device_on = False
                    time.sleep(.2)
                    oled.poweroff()
                    self.smg_temp.show("    ")
                    self.smg_humidity.show("    ")
                    self.led_turned_off()
            else:
                pass
    
if __name__ == '__main__':
    run = Main()
    run.main()
